﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IQ.DbBase;
using SqlServerAdmin;
using IQ.BaseWindows;

namespace IQ.DbA
{
    public partial class FrmMtSpNoTable : Form
    {
		#region # Constructor # 
        public FrmMtSpNoTable()
        {
            InitializeComponent();
            grdDead.AutoGenerateColumns = false;
        }
		#endregion
		
		#region - Private variables - 
        private SqlDeadSp deadSp = new SqlDeadSp();
		#endregion 

		#region - Private methods - 
        private void setMaxProgress(int value)
        {
            pgbProg.Maximum = value;
        }
        private void setProgress(int value)
        {
            pgbProg.Value = value;
            Application.DoEvents();
        }
        #endregion 
		
		//#region - Public properties / variables - 
		//#endregion

		//#region - Public methods - 
		//#endregion
		
		#region - Bind Methods - 
		#endregion
		
		#region - Form Events -
		// This region is for event handler methods that are called without the user knowing about it.
		// Such as the page loading.
        private void FrmMtSpNoTable_Load(object sender, EventArgs e)
        {
            if (DbHelper.CanConnect)
            {
                deadSp.SetMaxPosition += setMaxProgress;
                deadSp.SetCurrentPosition += setProgress;
            }
        }
        #endregion 
		
		#region - Control Methods -
		// This region is for event handler methods that are called knowingly by the user.
		// Such as a button being clicked.
        private void btnFindIt_Click(object sender, EventArgs e)
        {
            deadSp.RunProcess();
            grdDead.DataSource = deadSp.DeadTables;
            //grdDead.ReadOnly = false;
            grdDead.AllowUserToDeleteRows = true;
        }
        private void grdDead_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (grdDead.CurrentRow != null)
            {
                deadSp.Load_Sp_Text(grdDead.CurrentRow.Cells[cdSpName.Index].Value.ToString());
                txtSp.Text = deadSp.CurrentSpText;
                lblSpName.Text = grdDead.CurrentRow.Cells[cdSpName.Index].Value.ToString();
                txtDeadTable.Text = grdDead.CurrentRow.Cells[cdTableName.Index].Value.ToString();
                txtNewTable.Text = txtDeadTable.Text;
            }
            else
                General.MesDlg("Please select an item.", 'I');
        }

        private void btnDelSp_Click(object sender, EventArgs e)
        {
            if (grdDead.CurrentRow != null)
            {
                if (General.MesDlg("Delete server store proc: " + grdDead.CurrentRow.Cells[cdSpName.Index].Value.ToString() + " ?", 'C')
                    == DialogResult.Yes)
                    deadSp.Delete_Sp(grdDead.CurrentRow.Cells[cdSpName.Index].Value.ToString());
            }
            else
                General.MesDlg("Please select an item to delete.", 'I');
        }
        private void btnReplace_Click(object sender, EventArgs e)
        {
            txtSp.Text = txtSp.Text.Replace(txtDeadTable.Text, txtNewTable.Text);
            General.MesDlg("Replace complete.", 'I');
        }
        private void btnUpdSp_Click(object sender, EventArgs e)
        {
            if (grdDead.CurrentRow != null)
            {
                if (General.MesDlg("Delete server store proc: " + lblSpName.Text + " ?", 'C')
                    == DialogResult.Yes)
                {
                    if (deadSp.UpdateCurrentSp())
                        General.MesDlg("Update complete.", 'I');
                    else
                        General.MesDlg("Error: " + deadSp.LastMessage, 'E');
                }
            }
            else
                General.MesDlg("Please select an item to update.", 'I');            
        }
        #endregion
    }
}
